#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年3月17日

@author: chenyongbing
'''
import sys,os,time
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]
import datetime
from localConfig import LocalConfig
myLocalConfig = LocalConfig()

import logging

try:
    logger = logging.getLogger('base.copy_file_for_udisk')
except:
    logger = logging.basicConfig()



def get_slist_between_st_et(st,et):
    u'''获取2个时间间的所有日期'''
    dlist = []
    count = 0
    nday = (datetime.date(int(st[:4]),int(st[5:7]),int(st[8:]))+ datetime.timedelta(-1)).strftime('%Y-%m-%d')
    while nday < et:
        nday = (datetime.date(int(st[:4]),int(st[5:7]),int(st[8:]))+ datetime.timedelta(count)).strftime('%Y-%m-%d')
        count+=1
        dlist.append(nday)
    return dlist


if __name__ == '__main__':
    import argparse,re,commands
    parser = argparse.ArgumentParser(description='args')
    parser.add_argument('--day',metavar=u"day",default=None)
    parser.add_argument('--startTime',metavar=u"day",default=None)
    parser.add_argument('--endTime',metavar=u"day",default=None)
    args = parser.parse_args()
    day = args.day
    startTime = args.startTime
    endTime = args.endTime
    if day!=None:
        days = [day]
    else:
        if startTime==None:
            startTime = (datetime.date.today()).strftime('%Y-%m-%d')
        if endTime==None:
            endTime = (datetime.date.today()).strftime('%Y-%m-%d')
        days = get_slist_between_st_et(startTime, endTime)
        
    for day in days:
        if day==None:
            today = (datetime.date.today() ).strftime('%Y-%m-%d')
            yestday = (datetime.date.today() - datetime.timedelta(1)).strftime('%Y-%m-%d')
        else:
            today = day
            yestday = (datetime.date(int(day[:4]),int(day[5:7]),int(day[8:10])) - datetime.timedelta(1)).strftime('%Y-%m-%d')
            
        rawdata_path = myLocalConfig.rawdata_path
        employee_path = myLocalConfig.employee_path
        
        rawdata_back_path = '/Udisk/rawdata'
        employee_back_path = '/Udisk/Mysql/zhimind/mac_employee'
        
        
        rawfile = 'rawdata.%s.txt.tar.gz'%yestday
        emfile =  'mac_employee.%s.sql'%today
        while 1:
            if os.path.exists(rawdata_path+'/'+rawfile):
                logger.info( '%s already exists'%(rawfile))
                break
            else:
                commands.getstatusoutput('cp %s/%s %s'%(rawdata_back_path,rawfile,rawdata_path+'/'+rawfile))
            time.sleep(60)
        while 1:
            if os.path.exists(employee_path+'/'+emfile):
                logger.info( "%s already exists"%(emfile))
                break
            else:
                commands.getstatusoutput('gunzip -c %s > %s'%(employee_back_path+'/'+emfile+'.gz',employee_path+'/'+emfile))
            time.sleep(60)
#         logger.info( "Filter File %s Start."%rawfile)
#         os.popen('python %s --day %s'%(os.path.join(current_dir,'filterRawdata.py'),yestday))
#         logger.info( "Filter File %s End."%rawfile)
#         logger.info( "Identify Role(%s) Start."%yestday)
#         os.popen('python %s --startTime %s --endTime %s'%(os.path.join(current_dir,'runIdentifyEveryDay.py'),day,day))
#         logger.info( "Identify Role(%s) End."%yestday)    
            
        